package com.batchaps.classes;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class My2DSprite extends MyEntity {

	private Image[] _texture2D;
	private int _ntexture2D;
	private int _itexture2D;

	private float _posY;
	private float _posX;
	private float _velovityX;
	private float _velocityY;
	private float width;
	private float height;
	
	public boolean visible;
	public boolean isHower;

	public float get_velovityX() {
		return _velovityX;
	}

	public void set_velovityX(float _velovityX) {
		this._velovityX = _velovityX;
	}

	public float get_velocityY() {
		return _velocityY;
	}

	public void set_velocityY(float _velocityY) {
		this._velocityY = _velocityY;
	}

	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	public boolean isPlay() {
		return isPlay;
	}

	public void setPlay(boolean isPlay) {
		this.isPlay = isPlay;
	}

	// isplay
	public boolean isPlay;

	public My2DSprite(Image[] teture2d, float posX, float posY) {
		_texture2D = teture2d;
		_posX = posX;
		_posY = posY;
	}

	public My2DSprite(String teture2d, int teturesize, String texturetype,
			float posX, float posY , float w , float h) {
		_texture2D = new Image[teturesize];
		for (int i = 0; i < teturesize; i++) {
			try {
				_texture2D[i] = Image.createImage(teture2d + String.valueOf(i)
						+ texturetype);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		_itexture2D = 0;
		_ntexture2D = teturesize;
		_posX = posX;
		_posY = posY;
		isPlay = true;
		width = w;
		height = h;
	}

	public My2DSprite(String teture2d, int xB, int yB , float w , float h) {
		_texture2D = new Image[1];

		try {
			_texture2D[0] = Image.createImage(teture2d);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		_itexture2D = 0;
		_ntexture2D = 1;
		_posX = xB;
		_posY = yB;
		width = w;
		height = h;
	}
	
	public boolean CollidesWith(My2DSprite sprite)
    {
        if (visible && sprite.visible)
        {
            float redundantW1 = width * 20 / 100;
            float redundantH1 = height * 20 / 100;
            float redundantW2 = sprite.width * 20 / 100;
            float redundantH2 = sprite.height * 20 / 100;
            float top1 = _posX + redundantH1;
            float bottom1 = _posY + height - redundantH1;
            float left1 = _posX + redundantW1;
            float right1 = _posX + width - redundantW1;
            float top2 = sprite._posY + redundantH2;
            float bottom2 = sprite._posY + sprite.height - redundantH2;
            float left2 = sprite._posX + redundantW2;
            float right2 = sprite._posX + sprite.width - redundantW2;
            if (bottom1 >= top2 && top1 <= bottom2 && left1 <= right2 && right1 >= left2)
                return true;
        }
        return false;
    }

	public void Update() {
		if (isPlay) {
			_itexture2D = (_itexture2D + 1) % _ntexture2D;
		}
	}

	public void Draw(Graphics g, float gametime) {
		if (isPlay)
			g.drawImage(_texture2D[get_itexture2D()], (int) _posX, (int) _posY,
					0);
	}

	public Image[] get_texture2D() {
		return _texture2D;
	}

	public void set_texture2D(Image[] _texture2D) {
		this._texture2D = _texture2D;
	}

	public int get_ntexture2D() {
		return _ntexture2D;
	}

	public void set_ntexture2D(int _ntexture2D) {
		this._ntexture2D = _ntexture2D;
	}

	public int get_itexture2D() {
		return _itexture2D;
	}

	public void set_itexture2D(int _itexture2D) {
		this._itexture2D = _itexture2D;
	}

	public float get_posY() {
		return _posY;
	}

	public void set_posY(float _posY) {
		this._posY = _posY;
	}

	public float get_posX() {
		return _posX;
	}

	public void set_posX(float _posX) {
		this._posX = _posX;
	}
}
